#include "Sprite.h"


CSprite::CSprite()
{
	m_totalTiled = 0;
	m_widthTiled = 0;
	m_heightTiled = 0;
	m_columnTiled = 0;
	m_widthImage = 0;
	m_heightImage = 0;
	m_index = 0;
	m_running = false;
	m_duration = 100;
	m_durationCount = 0;
	m_firstIndex = 0;
	m_lastIndex = m_totalTiled - 1;
	m_loopTotal = -1;
	m_loopCount = 0;
	m_flag = true;
	m_flipY = false;
	m_indexStop = -1;
}
CSprite::CSprite(int _width, int _height)
{
	m_totalTiled = 1;
	m_widthTiled = _width;
	m_heightTiled = _height;
	m_widthImage = _width;
	m_heightImage = _height;
	m_columnTiled = 1;
	m_index = 0;
	m_running = false;
	m_duration = 100;
	m_durationCount = 0;
	m_firstIndex = 0;
	m_lastIndex = m_totalTiled - 1;
	m_loopTotal = -1;
	m_loopCount = 0;
	m_flag = true;
	m_flipY = false;
	m_indexStop = -1;
}

CSprite::CSprite(int _width, int _height, int _total, int _column)
{
	m_totalTiled = _total;
	m_widthImage = _width;
	m_heightImage = _height;	
	m_columnTiled = _column;
	m_widthTiled = _width/_column;
	int row = _total / _column;
	m_heightTiled = _height/row;
	m_index = 0;
	m_running = false;
	m_duration = 100;
	m_durationCount = 0;
	m_firstIndex = 0;
	m_lastIndex = m_totalTiled - 1;
	m_loopTotal = -1;
	m_loopCount = 0;
	m_flag = true;
	m_flipY = false;
	setIndex(m_index);
	m_indexStop = -1;
}
CSprite::~CSprite(void)
{
	delete m_texture;
}

void CSprite::init(LPDIRECT3DTEXTURE9 tex)
{
	m_texture = tex;
	setRECT();
}

void CSprite::draw(LPD3DXSPRITE sp)
{
	draw(sp, m_Pos,false);
}

void CSprite::draw(LPD3DXSPRITE sp, float posX, float posY,bool flipY)
{	
	draw(sp, D3DXVECTOR2(posX, posY),flipY);
}

void CSprite::draw(LPD3DXSPRITE sp, D3DXVECTOR2 _pos, bool flipY)
{
	draw(sp, _pos, D3DCOLOR_XRGB(255, 255, 255), flipY);
}

void CSprite::draw(LPD3DXSPRITE sp, D3DXVECTOR2 _pos, D3DCOLOR _color)
{
	D3DXVECTOR3 position = D3DXVECTOR3((float)_pos.x, (float)_pos.y, 0);
	sp->Draw(m_texture, &m_rect, NULL, &position, _color);
}



void CSprite::draw(LPD3DXSPRITE sp, D3DXVECTOR2 _pos, D3DCOLOR _color, bool flipY)
{
	D3DXMATRIX matrix;
	D3DXMatrixIdentity(&matrix);
	

	if (flipY == true)
	{
		D3DXVECTOR2 center_scale(_pos.x, _pos.y);
		D3DXVECTOR2 scale(-1, 1);

		D3DXMatrixTransformation2D(&matrix, &center_scale, 0, &scale, NULL, NULL, NULL);

		sp->SetTransform(&matrix);
	}

	
	
	D3DXVECTOR3 position = D3DXVECTOR3((float)_pos.x, (float)_pos.y, 0);
	if (flipY == true)
	{
		D3DXVECTOR3 center(getWidthTiled(), 0, 0);
		sp->Draw(m_texture, &m_rect, &center, &position, _color);

	}
	else
	{
		sp->Draw(m_texture, &m_rect, NULL, &position, _color);
	}

	if (flipY == true)
	{
		D3DXMatrixTransformation2D(&matrix, NULL, 0.0f, NULL, NULL, 0.0f, NULL);
		sp->SetTransform(&matrix);
	}
}

void CSprite::draw(LPD3DXSPRITE sp, D3DXVECTOR2 _pos, D3DXVECTOR3 _origin)
{
	D3DXVECTOR3 position = D3DXVECTOR3((float)_pos.x, (float)_pos.y, 0);
	sp->Draw(m_texture, &m_rect, &_origin, &position, D3DCOLOR_XRGB(255, 255, 255));
}

void CSprite::setRECT()
{
	m_rect.left = 0;
	m_rect.top = 0;
	m_rect.right = m_rect.left + m_widthTiled;
	m_rect.bottom = m_rect.top + m_heightTiled;
}

void CSprite::setRECT(int index)
{
	m_rect.left = (index % m_columnTiled) * m_widthTiled;
	m_rect.top = (index / m_columnTiled) * m_heightTiled;
	m_rect.right = m_rect.left + m_widthTiled;
	m_rect.bottom = m_rect.top + m_heightTiled;
}

void CSprite::setRECT(int l, int t, int r, int b)
{
	m_rect.left = l;
	m_rect.top = t;
	m_rect.right = r;
	m_rect.bottom = b;
}

void CSprite::update()
{
	if (isRunning())
	{
		m_durationCount++;
		if (m_durationCount >= m_duration)
		{
			m_durationCount = 0;
			if (m_flag)
			{
				m_index++;
				if (m_index > m_lastIndex)
				{
					m_index = m_firstIndex;
					if (m_loopTotal != -1)
					{
						m_loopCount++;
						if (m_loopCount > m_loopTotal)
						{
							if (m_indexStop < 0)
							{
								stopAnimate(m_lastIndex);
							}
							else
							{
								stopAnimate(m_indexStop);
							}
						}
					}
				}
			}
			else
			{
				m_index--;
				if (m_index < m_firstIndex)
				{
					m_index = m_lastIndex;
					if (m_loopTotal != -1)
					{
						m_loopCount++;
						if (m_loopCount > m_loopTotal)
						{
							if (m_indexStop < 0)
							{
								stopAnimate(m_lastIndex);
							}
							else
							{
								stopAnimate(m_indexStop);
							}
						}
					}
				}
			}
		}
	}
	setRECT(m_index);
}

void CSprite::updateLoop()
{
	if (isRunning())
	{
		m_durationCount++;
		if (m_durationCount >= m_duration)
		{
			m_durationCount = 0;
			if (m_flag)
			{
				m_index++;
				if (m_index > m_lastIndex)
				{
					m_index = m_firstIndex;					
				}
			}
			else
			{
				m_index--;
				if (m_index < m_firstIndex)
				{
					m_index = m_lastIndex;				
				}
			}
		}
	}
	setRECT(m_index);
}
void CSprite::animate(int _duration, int _fist, int _last, int _loop, int _indexStop)
{
	setRunning(true);
	m_indexStop = _indexStop;
	m_index = _fist;
	m_duration = _duration;
	m_durationCount = 0;
	m_firstIndex = _fist;
	m_lastIndex = _last;
	m_loopTotal = _loop;
	m_loopCount = 0;
	if (m_indexStop < 0)
	{
		m_flag = false;
	}
	else
	{
		m_flag = true;
	}

}

void CSprite::animate(int _duration, int _fist, int _last, int _loop, bool _flag)
{
	setRunning(true);
	if (_flag)
	{
		m_index = _fist;
	}
	else
	{
		m_index = _last;
	}
	m_duration = _duration;
	m_durationCount = 0;
	m_firstIndex = _fist;
	m_lastIndex = _last;
	m_loopTotal = _loop;
	m_loopCount = 0;
	m_flag = _flag;
}



void CSprite::animate(int _duration, int _fist, int _last, int _loop)
{
	animate(_duration, _fist, _last, _loop, true);
}

void CSprite::animate(int _duration, int _fist, int _last)
{
	animate(_duration, _fist, _last, -1);
}

void CSprite::animate(int _duration, int _loop)
{
	animate(_duration, 0, m_totalTiled - 1, _loop);
}

void CSprite::animate()
{
	animate(10, 0, m_totalTiled - 1, -1);
}

void CSprite::stopAnimate()
{
	stopAnimate(m_index);
}

void CSprite::stopAnimate(int i)
{
	m_index = i;
	m_loopCount = 0;
	m_loopTotal = 0;
	setRunning(false);
	setRECT(i);
}

